package com.wt.struggle.goodbye.user.activity

import android.app.AlertDialog
import android.os.Bundle
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.widget.EditText
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.*
import com.wt.struggle.goodbye.db.DbUtil
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.ProUserInfo
import com.wt.struggle.goodbye.user.adapter.AccountAdapter
import kotlinx.android.synthetic.main.change_account_layout.*
import kotlinx.android.synthetic.main.head.*
import org.json.JSONObject

class ChangeAccountActivity : ProActivity() {

    var dbUtil: DbUtil? = null

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {
            Send.LOGIN -> {

                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult

                    if (result != null && result != "" && result != "null") {

                        val jsonObject = JSONObject(result)
                        val token = jsonObject.optString("token")
                        val uid = jsonObject.optInt("uid")

                        Share.saveToken(this@ChangeAccountActivity, token)
                        Share.saveUid(this@ChangeAccountActivity, uid)
                        Share.saveAccount(this@ChangeAccountActivity, chooseInfo!!.account!!)

                        send!!.loginActivity(token,Share.getUmToken(this@ChangeAccountActivity), handler)

                    }

                }else{

                    removeLoadDialog()
                    showToastShort(netInfo.dataResult!!)
                }

            }

            Send.LOGIN_ACTIVITY -> {

                removeLoadDialog()

                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "") {
                        val json = JSONObject(result)
                        val uuid=json.optString("uuid")

                        Share.saveUuid(this,uuid)

                        finish()

                    }
                }
            }
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.change_account_layout)

        dbUtil = DbUtil(this)

        imageBack.setOnClickListener {
            finish()
        }

        tvTitle.text = "切换账号"
        initAdapter()

        getAccount()

        addLinearLayout.setOnClickListener {
            addAccount()
        }

    }


    /**
     * 添加账号
     */
    private fun addAccount() {
        val addBuilder = AlertDialog.Builder(this)
        val view = layoutInflater.inflate(R.layout.add_account_layout, null)
        val etAddAccount = view.findViewById<EditText>(R.id.etAddAccount)
        val etAddPwd = view.findViewById<EditText>(R.id.etAddPwd)
        addBuilder.setView(view).setPositiveButton("确定") { dialog, _ ->
            val account = etAddAccount.text.toString()
            val pwd = etAddPwd.text.toString()
            when {
                account == "" -> showToastShort("请输入账号")
                pwd == "" -> showToastShort("请输入账号密码")

                else -> {
                    val userInfo = ProUserInfo()
                    userInfo.account = account
                    userInfo.pwd = pwd

                    val message=dbUtil!!.addAccount(userInfo)
                    showToastShort(message)

                    getAccount()
                }
            }
            dialog.dismiss()

        }.setNegativeButton("取消") { _, _ ->

        }.show()

    }

    /**
     * 获取账号
     */
    private fun getAccount() {
        val arr = dbUtil!!.getAccountList()

        accountAdapter.updateDataClear(arr)

    }

    lateinit var accountAdapter: AccountAdapter

    val list = arrayListOf<ProUserInfo>()
    var chooseInfo: ProUserInfo? = null

    private fun initAdapter() {
        accountRecyclerView.layoutManager = LinearLayoutManager(this)
        accountRecyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        accountAdapter = AccountAdapter(this, list)
        accountRecyclerView.adapter = accountAdapter

        accountAdapter.itemClickListener = object : ItemClickListener {
            override fun onItemClick(position: Int) {

                chooseInfo = list[position]

                send!!.loginByPwd(
                    chooseInfo!!.account!!,
                    chooseInfo!!.pwd!!,
                    getImei(this@ChangeAccountActivity),
                    handler!!
                )
                showLoadDialog("登录中")

            }

            override fun onLongClick(position: Int) {

            }

        }
    }
}